﻿(function ($1, undefined) {
    $.sysRes = {
        iframeContainer: '#iframeContainer',
        iframeName: '#mainFrame',
        mainHeaderName: '#desktopHeader',
        mainFooterName: '#desktopFooter',
        sysLogoName: '#sysLogo',
        dialogName: '#sysDialog',
        dialogFrameName: '#dialogFrame'
    };
    $.extend({ removePrefix: function (nameStr) {
        return nameStr.replace('#', '');
    }
    });
    $.bs = {
        getWidthAndHeigh: function (resize) {
            var iframeHeight = document.body.clientHeight;
            var iframeWidth = document.body.clientWidth;
            // chrome,opera
            if ($.browser.safari || $.browser.opera) {
                iframeWidth -= 16;
                iframeHeight -= 85;
            }
            // firefox
            else if ($.browser.mozilla) {
                iframeWidth -= 15;
                iframeHeight -= 85;
                if (resize) {
                    iframeWidth += 18;
                    iframeHeight += 13;
                }
            }
            // IE
            else if ($.browser.msie) {
                iframeWidth -= 12;
                iframeHeight -= 83;
                if (resize) {
                    iframeHeight += 15;
                    iframeWidth += 10;
                }
            }
            return { width: iframeWidth, height: iframeHeight };
        },
        getContainerWidthAndHeight: function (containerName, resize) {
            var containerEle = document.getElementById(containerName) || window.parent.document.getElementById(containerName);
            if (resize == undefined)
                resize = true;
            if (containerEle != null) {
                var iframeHeight = containerEle.clientHeight;
                var iframeWidth = containerEle.clientWidth;
                // chrome,opera
                if ($.browser.safari || $.browser.opera) {
                    iframeWidth -= 16;
                    iframeHeight -= 85;
                }
                // firefox
                else if ($.browser.mozilla) {
                    iframeWidth -= 15;
                    iframeHeight -= 85;
                    if (resize) {
                        iframeWidth += 18;
                        iframeHeight += 13;
                    }
                }
                // IE
                else if ($.browser.msie) {
                    iframeWidth -= 12;
                    iframeHeight -= 83;
                    if (resize) {
                        iframeHeight += 15;
                        iframeWidth += 10;
                    }
                }
                return { width: iframeWidth, height: iframeHeight };
            }
            return { width: iframeWidth, height: iframeHeight };
        },
        iFrameReSizeHeight: function (iframename) {
            var pTar = document.getElementById(iframename);
            if (pTar) {
                if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) {
                    pTar.height = pTar.contentDocument.body.offsetHeight + 20;
                }
                else if (pTar.Document && pTar.Document.body.scrollHeight) {
                    pTar.height = pTar.Document.body.scrollHeight + 20;
                }
            }
        },
        iFrameReSizeWidth: function (iframename) {
            var pTar = document.getElementById(iframename);
            if (pTar) {
                if (pTar.contentDocument && pTar.contentDocument.body.offsetWidth) {
                    pTar.width = pTar.contentDocument.body.offsetWidth;
                }
                else if (pTar.Document && pTar.Document.body.scrollWidth) {
                    pTar.width = pTar.Document.body.scrollWidth;
                }
            }
        },
        showDialog: function (options) {
            var defaults = {
                url: '',
                autoOpen: true,
                buttons: {},
                closeOnEscape: true,
                closeText: '关闭',
                dialogClass: '',
                draggable: true,
                hide: null,
                height: 'auto',
                maxHeight: false,
                maxWidth: false,
                minHeight: 150,
                minWidth: 150,
                modal: true,
                position: {
                    my: 'center',
                    at: 'top',
                    collision: 'fit',
                    // ensure that the titlebar is never outside the document
                    using: function (pos) {
                        var topOffset = $(this).css(pos).offset().top;
                        if (topOffset < 0) {
                            $(this).css('top', pos.top - topOffset);
                        }
                    }
                },
                resizable: true,
                show: null,
                stack: true,
                title: '',
                width: 900,
                zIndex: 1000,
                showContainer: 'parent'
            };
            var opts = $.extend(defaults, options);
            var $eleDiv = $("<div id='" + $.removePrefix($.sysRes.dialogName) + "' style='overflow:hidden;'></div>");
            var iframeStr = "<iframe id='" + $.removePrefix($.sysRes.dialogFrameName) + "' name='";
            iframeStr += $.sysRes.dialogFrameName + "' src='" + opts.url + "'";
            iframeStr += " onload=\"$.bs.iFrameReSizeHeight('" + $.removePrefix($.sysRes.dialogFrameName) + "');\" width='100%'";
            iframeStr += " allowtransparency='true' frameborder='0' />";
            $eleDiv.append(iframeStr);
            if (opts.showContainer == "seft") {
                $($.sysRes.dialogName).remove();
                $($eleDiv).dialog(opts);
            }
            else if (opts.showContainer == "parent") {
                window.parent.$($.sysRes.dialogName).remove();
                window.parent.$($eleDiv).dialog(opts);
            }
        },
        closeDialog: function () {
            if (window.parent.$($.sysRes.dialogName).size() > 0)
                window.parent.$($.sysRes.dialogName).dialog('close');
            else
                $($.sysRes.dialogName).dialog('close');
        },
        showMsg: function (title, msgStr) {
            if (msgStr == undefined)
                msgStr = "无";
            /*(window.parent || window).$(".pageTip,#alertmod").remove();
            var $eleDiv = $("<div class='ui-widget ui-state-highlight ui-corner-all pageTip'></div>");
            var $eleP = $("<p><span class='ui-icon ui-icon-info' style='float: left; margin-right:0.3em;'></span></p>");
            $eleDiv.append($eleP.append("<strong>" + title + "：</strong>" + msgStr));
            (window.parent || window).$("body").append($eleDiv);


            $eleDiv.show("blind", 1000, function () {
            setTimeout(function () {
            (window.parent || window).$('.pageTip').hide('fade', 500);
            }, 1500);
            });*/
            (window.parent || window).$.pnotify({
                pnotify_text: msgStr,
                pnotify_delay: 3000,
                pnotify_history: false,
                pnotify_notice_icon: 'ui-icon ui-icon-info',
                pnotify_animation: { effect_in: 'show', effect_out: 'fade' },
                pnotify_stack: { "dir1": "down", "dir2": "left", "push": "top", "firstpos1": 20, "firstpos2": 15 }
            });
        },
        showErr: function (title, msgStr) {
            if (title == undefined)
                title = "错误信息";
            if (msgStr == undefined)
                msgStr = "无";
            /*(window.parent || window).$('.pageTip,#alertmod').remove();
            var $eleDiv = $("<div class='ui-widget ui-state-error ui-corner-all pageTip'></div>");
            var $eleP = $("<p><span class='ui-icon ui-icon-alert' style='float: left; margin-right:0.3em;'></span></p>");
            $eleDiv.append($eleP.append("<strong>" + title + "：</strong>" + msgStr));
            (window.parent || window).$("body").append($eleDiv);

            $eleDiv.show("blind", 2000, function () {
            setTimeout(function () {
            (window.parent || window).$('.pageTip').hide('fade', 500);
            }, 1500);
            });*/
            (window.parent || window).$.pnotify({
                pnotify_title: title,
                pnotify_text: msgStr,
                pnotify_delay: 3000,
                pnotify_history: false,
                pnotify_type: 'error',
                pnotify_notice_icon: 'ui-icon ui-icon-signal-diag',
                pnotify_stack: { "dir1": "down", "dir2": "left", "push": "top", "firstpos1": 20, "firstpos2": 15 }
            });
        },
        loadInContainer: function (url, containerName) {
            $("#alertmod").remove();
            if (containerName == undefined)
                containerName = $.removePrefix($.sysRes.iframeName);
            var containerFrame = window.frames[containerName] || parent.window.frames[containerName];
            containerFrame.location.href = url;
        }
    };
})(jQuery);